package com.Main.faislamovers;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.MarshalBase64;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("DefaultLocale")
public class GetTicketDetail extends Activity {

	String travel_id, source, destination, depature_time, arrival_time, date,
			seats, user_name, phone_no;
	String NAMESPACE = "http://tempuri.org/";
	String METHODNAME = "DeleteReservation";
	String url = "http://10.0.2.2:53512/Service1.asmx";
	String SopaAction = "http://tempuri.org/DeleteReservation";
	static String INFO_TAG = "[FM_INFO]";
	String rsult = null;
	private String user_id;
	
	ProgressDialog dialog_new;
	ProgressBar progressBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.getticketdetails);

		travel_id = getIntent().getStringExtra(AppConstants.TRAVEL_ID);
		source = getIntent().getStringExtra(AppConstants.SOURCE);
		destination = getIntent().getStringExtra(AppConstants.DESTINATION);
		depature_time = getIntent().getStringExtra(AppConstants.DEPARTURE_TIME);
		arrival_time = getIntent().getStringExtra(AppConstants.ARRIVAL_TIME);
		date = getIntent().getStringExtra(AppConstants.DATE);
		seats = getIntent().getStringExtra(AppConstants.SEAT_NO);

		SharedPreferences pref = getSharedPreferences(
				AppConstants.PREFERENCES_NAME, 0);
		user_name = pref.getString(AppConstants.USER_NAME, null);
		phone_no = pref.getString(AppConstants.PHONE_NO, null);
		user_id = pref.getString(AppConstants.USER_ID, null);

		((TextView) findViewById(R.id.source_to_destination)).setText(source
				.toUpperCase() + " To " + destination.toUpperCase());

		
		String date_travle = date.replace("/Date(", "");
		date_travle = date_travle.replace(")/", "");
		long l = Long.parseLong(date_travle);

		Date date_time = new Date(l);
		SimpleDateFormat df2 = new SimpleDateFormat("dd-MM-yyy");
		String final_date = df2.format(date_time);
		
		((TextView) findViewById(R.id.date)).setText(final_date);
		
		String new_depateure_time = depature_time.replace("/Date(", "");
		new_depateure_time = new_depateure_time.replace(")/", "");
		
		
		long l2 = Long.parseLong(new_depateure_time);

		Date dept_date_time = new Date(l2);
		SimpleDateFormat df = new SimpleDateFormat("hh:mm:aa");
		String final_dept_time = df.format(dept_date_time);
		
		String new_arravel_time = arrival_time.replace("/Date(", "");
		new_arravel_time = new_arravel_time.replace(")/", "");
		
		
		long l3 = Long.parseLong(new_arravel_time);

		Date arravel_date_time = new Date(l3);
		SimpleDateFormat df3 = new SimpleDateFormat("hh:mm:aa");
		String arravel_dept_time = df3.format(arravel_date_time);
		
		

		((TextView) findViewById(R.id.txt_reserve_info))
				.setText("User having Departure time " + final_dept_time
						+ " and their Arrival time " + arravel_dept_time.toUpperCase()
						+ " and he Reserved the seats " + seats);
		((TextView) findViewById(R.id.user_info)).setText("The user name is "
				+ user_name + " and phone no is " + phone_no);

		Button btn_delect_seat = (Button) findViewById(R.id.btn_delect_seat);
		btn_delect_seat.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				AlertDialog.Builder builder = new AlertDialog.Builder(
						GetTicketDetail.this);
				builder.setMessage("Are you want to Cancel the Reservation?");
				builder.setCancelable(false);
				builder.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
						
					
					
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								
								dialog_new = new ProgressDialog(GetTicketDetail.this);
								dialog_new.setMessage("Loading...");
								dialog_new.show();
								new Thread(new Runnable() {

									@Override
									public void run() {
										SoapObject request = new SoapObject(
												NAMESPACE, METHODNAME);
										request.addProperty("customer_id",
												Integer.parseInt(user_id));
										request.addProperty("travel_id",
												Integer.parseInt(travel_id));

										SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
												SoapEnvelope.VER11);
										envelope.dotNet = true;
										MarshalBase64 m = new MarshalBase64();
										m.register(envelope);
										envelope.encodingStyle = SoapEnvelope.ENC;
										// send request
										envelope.setOutputSoapObject(request);
										HttpTransportSE androidHttpTransport = new HttpTransportSE(
												url);
										try {
											androidHttpTransport.call(
													SopaAction, envelope);
										} catch (IOException e) {
											e.printStackTrace();
										} catch (XmlPullParserException e) {
										
											e.printStackTrace();
										}

										try {
											rsult = envelope.getResponse()
													.toString();
										} catch (SoapFault e) {
											
											e.printStackTrace();
										}

										runOnUiThread(new Runnable() {

											@Override
											public void run() {
												
												if (dialog_new.isShowing())
													dialog_new.hide();
												if (rsult.equals("Success")) {
													Context context = getApplicationContext();
													CharSequence text = "You are Successfully Delete your Resrvation !";
													int duration = Toast.LENGTH_SHORT;
													Toast toast = Toast
															.makeText(context,
																	text,
																	duration);
													toast.show();
													
												} else {
													Toast.makeText(
															getApplicationContext(),
															"Error in deletion",
															Toast.LENGTH_LONG)
															.show();
												}
												
												startActivity(new Intent( GetTicketDetail.this , MyBooking.class));
											}
										});

									}
								}).start();
								
								

							}
							
						
							
							
						}
				);

				
				
				builder.setNegativeButton("No",
						new DialogInterface.OnClickListener() {
						
							@Override
							public void onClick(DialogInterface dialog,
									int which) {

								dialog.cancel();
							}
						});

				AlertDialog alert = builder.create();
				alert.show();
			}
		});
		
		
		
	}
	
	@SuppressWarnings("unused")
	private void addProgressBar() {
		progressBar = new ProgressBar(this, null,
				android.R.attr.progressBarStyleSmall);
		this.addContentView(progressBar, new LayoutParams(50, 50));
		// progressBar.startAnimation(null);
	}
	@SuppressWarnings("unused")
	private class FetchOrdersAsyncTask extends AsyncTask<Void, Void, Void> {
		ProgressDialog dialog;
		Context ctx;

		public FetchOrdersAsyncTask(Context contxt) {
			this.ctx = contxt;
		}

		@Override
		protected void onPreExecute() {

			dialog_new = new ProgressDialog(this.ctx);
			dialog_new.setMessage("Loading...");
			if (!dialog_new.isShowing())
				dialog_new.show();

			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(Void... params) {

			return null;
		}

		@Override
		protected void onPostExecute(Void result) {

			super.onPostExecute(result);
		}
	}

}
